//
//  ScanAnimationView.m
//  QRCodeDemo
//
//  Created by zhoutl on 14-8-13.
//  Copyright (c) 2014年 zhoutl. All rights reserved.
//


#import "ScanAnimationView.h"

#define kAnimationKeyPath @"position"
#define kCornerLength 15.0f

@interface ScanAnimationView()<CAAnimationDelegate>
{
    CALayer *scanLineLayer_;
}


@end

@implementation ScanAnimationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    
    float scanFrameWidth  = kScreenWidth  * 3.0f / 4.0f;
    float scanFrameheight = kScreenWidth  * 3.0f / 4.0f;
    CGRect bounds = self.bounds;
    
    CGRect scanFrame = CGRectMake((bounds.size.width - scanFrameWidth) / 2.0f, (bounds.size.height - scanFrameheight)*2 / 5.0f, scanFrameWidth, scanFrameheight);

   CALayer  *contentLayer = [CALayer layer];
    contentLayer.frame = scanFrame;
    
    scanLineLayer_ = [CALayer layer];
    scanLineLayer_.frame = CGRectMake(0, 0, kAutoDeviceWidth(240), kAutoDeviceWidth(50));
    scanLineLayer_.contents = (id)[UIImage imageNamed:@"QRScanLine"].CGImage;
    scanLineLayer_.position = CGPointMake(80, -40);
    
    [contentLayer addSublayer:scanLineLayer_];
    [contentLayer setMasksToBounds:YES];
    
    [self.layer addSublayer:contentLayer];
    
    self.scanFrame = scanFrame;
    
    [self addSubview:self.titleView];
}


- (UIView *)titleView
{
    if (!_titleView) {
        
        _titleView = [[ScanActivityView alloc] initWithFrame: self.scanFrame];
                
        _titleView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return _titleView;
}

- (void)startScanAnimation
{
    [self stopScanAnimation];

    CGPoint scanLineStartPosition, scanLineEndPosition;
    scanLineStartPosition = CGPointMake(CGRectGetMidX(self.scanFrame) - kAutoDeviceWidth(40), CGRectGetMinY(self.scanFrame) - kAutoDeviceWidth(160));
    scanLineEndPosition = CGPointMake(CGRectGetMidX(self.scanFrame) - kAutoDeviceWidth(40), CGRectGetMaxY(self.scanFrame) - kAutoDeviceWidth(80));
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:kAnimationKeyPath];
    animation.delegate = self;
    [animation setFromValue:[NSValue valueWithCGPoint:scanLineStartPosition]];
    [animation setToValue:[NSValue valueWithCGPoint:scanLineEndPosition]];
    [animation setDuration:1.8f];
    [animation setRemovedOnCompletion:NO];
    [animation setRepeatCount:100000];
    [animation setFillMode:kCAFillModeForwards];
    
    [scanLineLayer_ addAnimation:animation forKey:kAnimationKeyPath];
}

- (void)stopScanAnimation
{
    if ([scanLineLayer_ animationForKey:kAnimationKeyPath]) {
        [scanLineLayer_ removeAnimationForKey:kAnimationKeyPath];
    }
}

#pragma mark-- 扫码相关提示框
- (void)showLoading
{
    self.titleView.messageTitle.text = @"已扫描，正在处理...";
    self.titleView.hidden = NO;
}

- (void)stopLoading
{
    self.titleView.hidden = YES;
    self.titleView.activityView.hidden = NO;
}

- (void)showNetWorkException
{
    self.titleView.messageTitle.text = @"当前网络不可用\n请检查网络设置";
    self.titleView.activityView.hidden = YES;
    self.titleView.hidden = NO;
}


- (void)drawRect:(CGRect)rect
{
    self.backgroundColor = [UIColor redColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    UIColor *color = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [color set];
    UIRectFill(self.bounds);
    
    UIColor *clearColor = [UIColor clearColor];
    [clearColor set];
    UIRectFill(self.scanFrame);
    
    //画边框
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextStrokeRectWithWidth(context, CGRectInset(self.scanFrame, 0, 0), 0.1f);
    
    //画边角
    CGPoint leftUp = CGPointZero;
    leftUp = CGPointMake(self.scanFrame.origin.x + 1 , self.scanFrame.origin.y +1);
    CGPoint rightUp = CGPointMake(CGRectGetMaxX(self.scanFrame)-1, leftUp.y);
    CGPoint rightDown = CGPointMake(rightUp.x, CGRectGetMaxY(self.scanFrame)-1);
    //左上角
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, leftUp.x, leftUp.y + kCornerLength);
    CGPathAddLineToPoint(path, NULL, leftUp.x, leftUp.y);
    CGPathAddLineToPoint(path, NULL, leftUp.x + kCornerLength, leftUp.y);
    //右上角
    CGPathMoveToPoint(path, NULL, rightUp.x - kCornerLength, rightUp.y);
    CGPathAddLineToPoint(path, NULL, rightUp.x, rightUp.y);
    CGPathAddLineToPoint(path, NULL, rightUp.x, rightUp.y + kCornerLength);
    //右下角
    CGPathMoveToPoint(path, NULL, rightDown.x, rightDown.y - kCornerLength);
    CGPathAddLineToPoint(path, NULL, rightDown.x, rightDown.y);
    CGPathAddLineToPoint(path, NULL, rightDown.x - kCornerLength, rightDown.y);
    //左下角
    CGPathMoveToPoint(path, NULL, leftUp.x, rightDown.y - kCornerLength);
    CGPathAddLineToPoint(path, NULL, leftUp.x, rightDown.y);
    CGPathAddLineToPoint(path, NULL, leftUp.x + kCornerLength, rightDown.y);
    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 2.0);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
    
    CGContextRestoreGState(context);
    
    CGPathRelease(path);
}
    
#pragma mark -- CAAnimationDelegate
- (void)animationDidStart:(CAAnimation *)anim
{
    DLog(@"开始动画");
}
    
    


@end
